package com.xm.dao.background;

import com.xm.entity.Model;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 总类别
 * @author 康文程
 * @date 2024-1-20 下午 01:07
 */
public class ModelDao extends BaseUtil {

    /**
     * 查询全部总类别
     *
     * @return List<Model> 总类别列表
     * @throws SqlErrorException SQL执行异常
     */
    public List<Model> findAll() throws SqlErrorException {
        @Language("SQL")
        String sql = "SELECT * FROM model";
        try {
            ResultSet rs = executeQuery(sql);
            List<Model> list = new ArrayList<>();
            while (rs.next()) {
                Model model = new Model();
                model.setModelId(rs.getInt("model_id"));
                model.setModelName(rs.getString("model_name"));
                list.add(model);
            }
            return list;
        } catch (SQLException e) {
            throw new SqlErrorException("查询总类别失败", e);
        }
    }
}
